package xiangya.bysj.client.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xiangya.bysj.entity.Patient;
import xiangya.bysj.entity.TakeInfo;
import xiangya.bysj.service.impl.HistoryTakeServiceImpl;
import xiangya.bysj.service.impl.PatientServiceImpl;
import xiangya.bysj.service.impl.TakeInfoServiceImpl;
import xiangya.bysj.util.JsonUtil;
import xiangya.bysj.util.StringUtil;

/**
 * 服药信息的controller
 * 
 * @author Mr.zhang
 *
 */
@Controller
@RequestMapping(value = "/client/takeinfo", produces = "application/json;charset=UTF-8")
public class TakeInfoClientController {

	@Resource(name = "takeInfoService")
	private TakeInfoServiceImpl takeInfoService;
	@Autowired
	private PatientServiceImpl patientService;
	@Autowired
	private HistoryTakeServiceImpl historyService;

	@RequestMapping("/add")
	@ResponseBody
	public String add(String reqJson) {
		String result = StringUtil.RES_FAIL;
		try {
			TakeInfo takeinfo = (TakeInfo) JsonUtil.getObject(reqJson, TakeInfo.class);
			takeInfoService.save(takeinfo);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			result = StringUtil.RES_FAIL;
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/update")
	@ResponseBody
	public String update(String reqJson) {
		String result = StringUtil.RES_FAIL;
		try {
			TakeInfo takeinfo = (TakeInfo) JsonUtil.getObject(reqJson, TakeInfo.class);
			takeInfoService.update(takeinfo);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(int takeinfoId) {
		String result = StringUtil.RES_FAIL;
		try {
			takeInfoService.delete(takeinfoId);
			result = StringUtil.RES_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/queryById")
	@ResponseBody
	public String queryById(int takeinfoId) {
		String result = StringUtil.RES_FAIL;
		try {
			if (takeinfoId > 0) {
				TakeInfo takeinfo = takeInfoService.getById(takeinfoId);
				result = JsonUtil.getJson(takeinfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = StringUtil.RES_FAIL;
		}
		return result;
	}

	@RequestMapping("/queryByUser")
	@ResponseBody
	public String queryByUser(int userId) {
		String result = StringUtil.RES_FAIL;
		try {
			if (userId > 0) {
				List<TakeInfo> list = takeInfoService.getByUser(userId);
				if (list != null) {
					result = JsonUtil.getJson(list);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = StringUtil.RES_FAIL;
		}
		return result;
	}

	@RequestMapping("/queryHist")
	@ResponseBody
	public Map<String, String> queryHistInfo(int userId) {
		Map<String, String> map = null;
		try {
			List<Patient> patients = patientService.findPatientsByUser(userId);
			int[] pIds = new int[patients.size()];
			for (int i = 0; i < pIds.length; i++) {
				pIds[i] = patients.get(i).getId();
			}
			List<TakeInfo> takeinfos = null;
			if (pIds.length > 0) {
				takeinfos = takeInfoService.getByPatients(pIds);
			} else {
				takeinfos = new ArrayList<TakeInfo>();
			}
			for (int i = 0; i < takeinfos.size(); i++) {
				TakeInfo info = takeinfos.get(i);
				if (historyService.getNumberByPatient(info.getPatientId(), info.getMedicId()) <= 0) {
					takeinfos.remove(i);
					i--;
				}
			}
			String jsonPatients = JsonUtil.getJson(patients);
			String jsonTakeinfo = JsonUtil.getJson(takeinfos);
			map = new HashMap<String, String>();
			map.put("takeinfo", jsonTakeinfo);
			map.put("patients", jsonPatients);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping("/auth")
	@ResponseBody
	public Object auth(@RequestParam(value = "doctorId", required = false) int doctorid,
			@RequestParam(value = "patientId", required = false) int patientid) {
		HashMap<String, Object> object = new HashMap<String, Object>();

		try {
			// Doctor doctor = doctorService.getById(doctorid);
			/**
			 * 
			 * 此处需验证医生权限
			 */
			object.put("code", "success");
			object.put("takeinfos", JsonUtil.getJson(takeInfoService.getByPatientId(patientid, null)));

		} catch (Exception e) {
			object.put("code", "fail");
			object.put("code", patientid);
			e.printStackTrace();
			return object;
		}

		return object;
	}

}
